//
//  ForgetPwddViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/11.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "ForgetPwddViewController.h"
#import "RegisterTableViewCell.h"

static int i = 29;
@interface ForgetPwddViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UILabel *getTestNumberLabel;
@property (nonatomic, strong) UIButton *sendCodeButton;
@property(nonatomic,strong) NSTimer *timmer;// 定时器
@end

@implementation ForgetPwddViewController
- (void)getAuthoCode{ // 获取验证码
    NSString *telephoneStr = self.dataArray[0][@"textFieldValues"];
    if (telephoneStr.length <= 0 || telephoneStr == nil || telephoneStr == NULL) {
        ShowMessage(@"请输入手机号");
        return;
    }
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=sendsms" parameters:@{@"phonenum":telephoneStr} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"data"][@"flag"] isEqualToString:@"1"]) {
            ShowMessage(@"操作成功");
        }else{
            ShowMessage(@"操作失败");
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}
- (void)forgetPwd {
    NSString *telephoneStr = self.dataArray[0][@"textFieldValues"];
    NSString *pwdStr = self.dataArray[2][@"textFieldValues"];
    NSString *makeSurePwdStr = self.dataArray[3][@"textFieldValues"];
    NSString *yzmStr = self.dataArray[1][@"textFieldValues"];
    if (telephoneStr.length <= 0 || telephoneStr == nil || telephoneStr == NULL) {
        ShowMessage(@"请输入手机号");
        return;
    }
    if ([pwdStr isEqualToString:@""] || [pwdStr isKindOfClass:NULL] || [pwdStr isKindOfClass:nil]) {
        ShowMessage(@"请输入密码");
        return;
    }
    NSString *passwordStr = [self md5:pwdStr];
    NSString *MakeSurepasswordStr = [self md5:makeSurePwdStr];
    
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=forget" parameters:@{@"mobile":telephoneStr,@"password":passwordStr,@"repassword":MakeSurepasswordStr,@"verifycode":yzmStr} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
            ShowMessage(@"操作成功");
        }else{
            NSString *messagestr = [NSString stringWithFormat:@"%@",responseObject[@"message"]];
            ShowMessage(messagestr);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}

- (void)registerBtnAction:(UIButton *)btn
{
    [self forgetPwd];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"密码找回";
    self.navigationController.navigationBarHidden = NO;
    [self.view addSubview:self.tableView];
}
#pragma mark - tableView的delegate和dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 70 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20 / HEIGHT_6S_SCALE;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 13/WIDTH_5S_SCALE)];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    //底
    UIView * footerView = [[UIView alloc]init];
    footerView.backgroundColor = [UIColor clearColor];
    //按钮
    UIButton * registerBtn = [[UIButton alloc] initWithFrame:CGRectMake(20 / WIDTH_6S_SCALE, 20 / HEIGHT_6S_SCALE, SCREEN_WIDTH - 40 /WIDTH_6S_SCALE, 42 / HEIGHT_6S_SCALE)];
    [registerBtn setBackgroundColor:getColor(mainColor)];
    registerBtn.titleLabel.font = DEF_FontSize_14;
    [registerBtn setTitle:@"确定" forState:UIControlStateNormal];
    [registerBtn setTintColor:[UIColor whiteColor]];
    registerBtn.layer.cornerRadius = 5;
    registerBtn.layer.masksToBounds = YES;
    [registerBtn addTarget:self action:@selector(registerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:registerBtn];
    return footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RegisterTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RegisterTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    cell.getTextFieldChangeBlock = ^(NSString *str) {
        if (str) {
            self.dataArray[indexPath.row][@"textFieldValues"] = str;
        }
    };
    if (indexPath.row == 1) {
        [cell.contentView addSubview:self.sendCodeButton];
        [self.sendCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView).offset(-10 / WIDTH_5S_SCALE);
            make.centerY.equalTo(cell.contentView);
            make.width.mas_offset(80/WIDTH_5S_SCALE);
            make.height.mas_offset(30/WIDTH_5S_SCALE);
        }];
    }
    if (indexPath.row == 0 || indexPath.row == 1) {
        cell.contentTextFidle.secureTextEntry = NO;
    }
    return cell;
}

#pragma mark - action
- (void)sendNumber{
    NSLog(@"发送验证码");
    [self.view endEditing:YES];
    [self getAuthoCode];
    self.sendCodeButton.enabled = NO;
    [self.sendCodeButton setTitle:@"已发送" forState:UIControlStateDisabled];
    //    [self.sendCodeButton setBackgroundColor:[UIColor grayColor]];
    //初始化定时器
    _timmer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test:) userInfo:nil repeats:YES];
}

- (void)test:(NSTimer *)timmer{
    NSString *str = [NSString stringWithFormat:@"%d后重试",i];
    [self.sendCodeButton setTitle:str forState:UIControlStateDisabled];
    i--;
    if (i == -1) {
        [timmer invalidate];
        self.sendCodeButton.enabled = YES;
        [self.sendCodeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
        [self.sendCodeButton setBackgroundColor:[UIColor clearColor]];
        i = 29;
    }
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[RegisterTableViewCell class] forCellReuseIdentifier:NSStringFromClass([RegisterTableViewCell class])];
        _tableView.tableFooterView = [UIView new];
        _tableView.bounces = NO;
    }return _tableView;
}
- (UIButton *)sendCodeButton
{
    if (!_sendCodeButton) {
        _sendCodeButton = [[UIButton alloc]init];
        _sendCodeButton.backgroundColor = [UIColor clearColor];
        [_sendCodeButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [_sendCodeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
        _sendCodeButton.titleLabel.font = DEF_FontSize_14;
        [_sendCodeButton addTarget:self action:@selector(sendNumber) forControlEvents:UIControlEventTouchUpInside];
        _sendCodeButton .layer.cornerRadius = 5;
        _sendCodeButton.layer.masksToBounds = YES;
        _sendCodeButton.layer.borderColor = getColor(mainColor).CGColor;
        _sendCodeButton.layer.borderWidth = 1.0;
    }
    return _sendCodeButton;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[@{@"placeHolder":@"手机号",@"img":@"logo_ico.1",@"textFieldValues":@""}.mutableCopy,
                       @{@"placeHolder":@"验证码",@"img":@"logo_ico.3",@"textFieldValues":@"123456"}.mutableCopy,
                       @{@"placeHolder":@"输入6位以上密码",@"img":@"logo_ico.2",@"textFieldValues":@""}.mutableCopy,
                       @{@"placeHolder":@"确认密码",@"img":@"logo_ico.2",@"textFieldValues":@""}.mutableCopy
                       ].mutableCopy;
    }
    return _dataArray;
}
@end

